package com.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.entity.wms.ClassifyEntity;
import com.group.entity.wms.GoodsEntity;
import com.group.entity.wms.PositionEntity;
import com.group.exception.IndustryException;
import com.group.mapper.GoodsMapper;
import com.group.mapper.PositionMapper;
import com.group.service.ClassifyService;
import com.group.service.PositionService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, PositionEntity> implements PositionService {
    @Resource
    @Lazy
    private ClassifyService classifyService;
    @Resource
    private GoodsMapper goodsMapper;
    /**
     * 根据库区id查找该库区下面所有的储位信息
     * @param warehouseAreaId
     * @return
     */
    @Override
    public List<PositionEntity> findListByWarehouseAreaId(Long warehouseAreaId) {
        return  this.baseMapper.findByWarehouseAreaId(warehouseAreaId);
    }

    /**
     * 根据储位类型自动生成储位编号
     * @param positionType
     * @return
     */
    @Override
    public String initPositionNumber(Long positionType) {
        return null;
    }

    @Override
    public List<PositionEntity> findAll() {
        List<ClassifyEntity> list = classifyService.getBaseMapper().selectList(new QueryWrapper<>());
        Map<Long, String> nameMap = list.stream().collect(Collectors.toMap(item -> item.getId(), item -> item.getClassifyName()));

        List<PositionEntity> positionEntities = this.baseMapper.selectList(null);
         return positionEntities.stream().peek(item->item.setPositionTypeName(nameMap.get(item.getPositionType())))
                 .collect(Collectors.toList());
    }

    /**
     * 添加储位信息
     * @param positionEntity
     * @return
     */
    @Override
    public Integer add(PositionEntity positionEntity) {
        //判断储位名称是否重复
        List<PositionEntity> position_name = this.baseMapper.selectList(new QueryWrapper<PositionEntity>().eq("position_name", positionEntity.getPositionName())
        .eq("position_number",positionEntity.getPositionNumber()));
        if (position_name!=null&&position_name.size()>=1) throw new IndustryException("储位名称或者，储位编号重复");
        positionEntity.setCreateTime(new Date());
        //查看储位编码是否重复
        PositionEntity position_bar_code = this.baseMapper.selectOne(new QueryWrapper<PositionEntity>().eq("position_bar_cod", positionEntity.getPositionBarCod()));
        if (position_bar_code!=null) throw new IndustryException("储位条码已经存在");
        positionEntity.setCreateUser(1024L);
        positionEntity.setCreateTime(new Date());
        boolean save = this.save(positionEntity);
        return save?1:0;
    }

    /**
     * 模糊查询
     * @param keyWorld
     * @return
     */
    @Override
    public List<PositionEntity> findByNameLike(String keyWorld) {
        return this.baseMapper.findByNameLike(keyWorld);
    }

    @Override
    public Integer delete(Long id) {
        //查看该储位下是否含有商品信息
        List<GoodsEntity> goodsEntityList = this.goodsMapper.selectList(new QueryWrapper<GoodsEntity>().eq("position_id", id));
        if (goodsEntityList!=null&&goodsEntityList.size()>=1) throw  new IndustryException("该储位下含有商品信息，无法删除");
        int i = this.baseMapper.deleteById(id);
        return i;
    }
}
